﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionTable
    {
        GestionNotification gestionNotification = new GestionNotification();

        public void appelServeur(Table table)
        {
            Notification notif = new Notification();
            notif.setType(TypeNotification.AppelServeur);
            notif.Fait = false;
            notif.Serveur = table.Zone.Serveur;
            gestionNotification.Ajouter(notif);
        }

        public void Ajouter(Table table)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Tables.AddObject(table);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public Table Chercher(int tableId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Tables.Where(t => t.Id == tableId).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        Table TrouverCommande(Commande commande)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from t in db.Tables
                            where t.Commande == commande 
                            select t.Commande.Table).FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erreur Repository : Echec Recuperation table avec commande : " + commande, ex);
            }
        }

        public List<Table> Lister(Zone zone)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from t in db.Tables.Include("Commande")
                            where t.Zone.Id == zone.Id
                            select t).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Table> Lister(Commande commandeEnCours)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from t in db.Tables
                            where t.Commande.Id == commandeEnCours.Id
                            select t).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Table> ListerTablesLibres(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from t in db.Tables
                            where t.estOccupe == false && t.Zone.Restaurant.Id == restaurant.Id
                            select t).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Table table)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Tables.Attach(table);
                    db.ObjectStateManager.ChangeObjectState(table, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Table table)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Tables.DeleteObject(table);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
